package com.wondersgroup.fjzlpt.org.dao.impl.jgqy.fwgl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.fwgl.FwglDAO;
@Repository
public class FwglDAOImpl extends BaseJTDAOImpl implements FwglDAO{
	@Override
	@Autowired
	@Qualifier("TB_JGQY_FWGL")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}

	/**
	 * 分页查询
	 */
	public JSONObject queryPage(Map<String, String> map) {
		JSONObject sqlObj = this.getPageSql(map);
		String sql = sqlObj.getString("sql");
		List<String> list = (List)sqlObj.get("sqlParams");
		
		PageQuerySQLBean sb = super.checkAndCreatePageQuerySQLBean(map, sql, "order by a.YQFWSJ DESC", list.toArray());
		return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
	}
	
	/**
	 * 根据条件查询所有数据
	 * @param map
	 * @return
	 */
	public JSONArray queryAll(Map<String, String> map){
		JSONObject sqlObj = this.getPageSql(map);
		String sql = sqlObj.getString("sql") + " order by a.YQFWSJ DESC";
		List<String> list = (List)sqlObj.get("sqlParams");
		
		SQLBean bean = new SQLBean(sql, list.toArray());
		return super.queryList(bean);
	}
	
	
	private JSONObject getPageSql(Map<String, String> map){
		JSONObject result = new JSONObject();
		
		StringBuffer sbuf = new StringBuffer();
		sbuf.append("select a.*,b.YLJGID,b.FWZT,c.FWMC,c.FWNR,c.XZBL,");
		sbuf.append("(select HZXM from TB_JGQY_BLXX where BLID = a.BLID) HZXM,");
		sbuf.append("(select ZDMC from TB_JGQY_BLXX where BLID = a.BLID) ZDMC,");
		sbuf.append("e.YLJGMC TGFWJGMC,");
		sbuf.append("(select YLJGMC from TB_ZD_YLJGXX where PTJGID = b.YLJGID) YLJGMC,");
		sbuf.append("(select XMMC from TB_ZY_YLTLX where XMDM = d.YLTLX) YLTLXMC \n");
		sbuf.append(" from TB_JGQY_FWGL a,TB_JGQY_FWGL_JSFWJG b,TB_XXWH_FWXX c,TB_JGQY_YLT d, TB_ZD_YLJGXX e  ");
		sbuf.append(" where a.FWGLID = b.FWGLID and a.FWID = c.FWID and a.YLTID = d.YLTID and e.PTJGID = a.TGFWJGID ");
		List list = new ArrayList();
		String value = map.get("FWMC");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and c.FWMC like ? ");
			list.add("%"+value+"%");
		}
		value = map.get("TGFWJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and a.TGFWJGID = ? ");
			list.add(value);
		}
		value = map.get("JGXZQHDM");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and e.XZQHDM like ? ");
			list.add(value+"%");
		}
		value = map.get("YLJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and b.YLJGID = ? ");
			list.add(value);
		}
		value = map.get("FWSJ");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and a.YQFWSJ = ? ");
			list.add(value);
		}
		/*value = map.get("JSSJ");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and a.YQFWSJ <= ? ");
			list.add(value);
		}*/
		value = map.get("FWGLID");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and a.FWGLID = ? ");
			list.add(value);
		}
		
		result.put("sql", sbuf.toString());
		result.put("sqlParams", list);
		
		return result;
	}

	/*@Override
	public JSONArray queryList(String[] fwglidAry,Map<String, String> map) {
		StringBuffer sbuf = new StringBuffer();
		sbuf.append("select a.*,b.YLJGID,b.FWZT,c.FWMC,c.FWNR,c.XZBL,");
		sbuf.append("(select HZXM from TB_JGQY_BLXX where BLID = a.BLID) HZXM,");
		sbuf.append("(select ZDMC from TB_JGQY_BLXX where BLID = a.BLID) ZDMC,");
		sbuf.append("(select YLJGMC from TB_ZD_YLJGXX where PTJGID = a.TGFWJGID) TGFWJGMC,");
		sbuf.append("(select YLJGMC from TB_ZD_YLJGXX where PTJGID = b.YLJGID) YLJGMC,");
		sbuf.append("(select XMMC from TB_ZY_YLTLX where XMDM = d.YLTLX) YLTLXMC \n");
		sbuf.append(" from TB_JGQY_FWGL a,TB_JGQY_FWGL_JSFWJG b,TB_XXWH_FWXX c,TB_JGQY_YLT d  ");
		sbuf.append(" where a.FWGLID = b.FWGLID and a.FWID = c.FWID and a.YLTID = d.YLTID and a.FWGLID in(");
		String params = "";
		List list = new ArrayList();
		for (String string : fwglidAry) {
			params += "?,";
			list.add(string);
		}
		params = params.substring(0, params.lastIndexOf(","));
		sbuf.append(params);
		sbuf.append(") \n");
		String value = map.get("TGFWJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and a.TGFWJGID = ? ");
			list.add(value);
		}
		value = map.get("YLJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and b.YLJGID = ? ");
			list.add(value);
		}
		SQLBean sb = new SQLBean();
		sb.setSql(sbuf.toString());
		sb.setParamAry(list);
		return super.queryList(sb);
	}*/


}
